package NewTmas.FramesandPanels;


import com.toedter.calendar.JDateChooser;
import taskmaster.auth.User;
import taskmaster.job.TaskBuilder;
import taskmaster.util.LookupLists;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class JobMonitorOpenerFrame extends JFrame
{

    private JPanel contentPane;

    User u;
    JList joblist;
    JList rolelist = new JList();
    String[] selectedCategory;

    LookupLists lookupLists;
    TaskBuilder tb;

    HashMap<String, Integer> roleMap;

    //size dimensions for the GraphViz graph
    public static int xdim[] = new int[]{0, 4, 8, 10, 12, 15, 18, 20};
    public static int ydim[] = new int[]{0, 4, 8, 10, 12, 15, 18, 20};


    public JobMonitorOpenerFrame(final User user, final LookupLists ll, final TaskBuilder tb, final JDesktopPane dtp, final OpsModeMainFrame opsModeMainFrame)
    {
        String[] usersJobs;


        setTitle("Job Monitor");


        this.u = user;
        this.lookupLists = ll;
        this.tb = tb;
        try
        {
            usersJobs = lookupLists.getUsersJobs();
            joblist = new JList(usersJobs);
            DefaultListModel model = new DefaultListModel();
            roleMap = ll.getreverseUserRolesHash();
            String[][] stringmap = ll.getUsersRoles();
            for (int i = 0; i < stringmap.length; i++)
            {
                model.addElement(stringmap[i][1]);
            }

            rolelist.setModel(model);

        } catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error retrieving values for user jobs and roles: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        setSize(1100, 900);
        setLocation(20, 20);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        getContentPane().add(contentPane, BorderLayout.CENTER);

        JLabel lblDate = new JLabel("Date:");
        lblDate.setBounds(764, 24, 40, 23);
        lblDate.setForeground(Color.BLUE);
        contentPane.add(lblDate);

        final JDateChooser datechooser = new JDateChooser();
        datechooser.setBounds(850, 24, 169, 23);
        datechooser.setDate(opsModeMainFrame.dateToSave);


        contentPane.add(datechooser, BorderLayout.NORTH);

        JButton btnSubmit = new JButton("Submit");

        btnSubmit.setBounds(739, 759, 101, 23);
        contentPane.add(btnSubmit);

        JLabel lblNewLabel = new JLabel("Roles");
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setBackground(Color.BLUE);
        lblNewLabel.setBounds(14, 44, 101, 14);
        contentPane.add(lblNewLabel);


        Collection<String> access = u.getAllRoles().values();
        HashSet<String> accessSet = new HashSet<String>();
        for (String str : access)
        {
            accessSet.add(str);
        }

        String[] accesslevels = new String[accessSet.size()];
        accessSet.toArray(accesslevels);

        JList Accesslist = new JList(accesslevels);
        Accesslist.setBounds(945, 394, 82, 55);
        contentPane.add(Accesslist);

        JLabel lblJobs = new JLabel("Jobs");
        lblJobs.setForeground(Color.BLUE);

        lblJobs.setBounds(248, 44, 82, 14);
        contentPane.add(lblJobs);

        contentPane.setPreferredSize(this.getSize()); //setPreferredSize(new Dimension(int length, int width))


        JLabel lblGraphLibrary = new JLabel("Graph Library");
        lblGraphLibrary.setForeground(Color.BLUE);
        lblGraphLibrary.setBounds(950, 502, 100, 14);
        contentPane.add(lblGraphLibrary);

        final JLabel XdimLabel = new JLabel("X Dimension");
        final JLabel YdimLabel = new JLabel("Y Dimension");

        final JComboBox XDim = new JComboBox();
        final JComboBox YDim = new JComboBox();

        XdimLabel.setForeground(Color.BLUE);
        YdimLabel.setForeground(Color.BLUE);

        XdimLabel.setBounds(950, 560, 100, 14);
        YdimLabel.setBounds(950, 617, 100, 14);
        XDim.setBounds(940, 587, 92, 20);
        YDim.setBounds(940, 640, 92, 20);

        contentPane.add(XDim);
        contentPane.add(XdimLabel);
        contentPane.add(YdimLabel);
        contentPane.add(YDim);

//        XdimLabel.setVisible(false);
//        YdimLabel.setVisible(false);
//        XDim.setVisible(false );
//        YDim.setVisible(false);


        for (int i : xdim)
        {
            XDim.addItem(i);
        }

        for (int i : ydim)
        {
            YDim.addItem(i);
        }

        final JComboBox comboBox = new JComboBox();

        comboBox.setModel(new DefaultComboBoxModel(opsModeMainFrame.libToSave));
        comboBox.setBounds(940, 527, 92, 20);
        contentPane.add(comboBox);

        if (comboBox.getSelectedItem().toString().equalsIgnoreCase("GraphViz"))
        {
            XdimLabel.setVisible(true);
            YdimLabel.setVisible(true);
            XDim.setVisible(true);
            YDim.setVisible(true);
        } else
        {
            XdimLabel.setVisible(false);
            YdimLabel.setVisible(false);
            XDim.setVisible(false);
            YDim.setVisible(false);

        }

        JScrollPane listscroller = new JScrollPane();
        listscroller.setBounds(236, 69, 195, 462);
        contentPane.add(listscroller);

        listscroller.setViewportView(joblist);

        JScrollPane rolescroller = new JScrollPane(rolelist);
        rolelist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        rolescroller.setBounds(14, 69, 177, 462);
        contentPane.add(rolescroller);

        JLabel lblAccesslevels = new JLabel("Access Levels");
        lblAccesslevels.setForeground(Color.BLUE);
        lblAccesslevels.setBounds(945, 369, 150, 14);
        contentPane.add(lblAccesslevels);


        JLabel lblNewLabel_1 = new JLabel("Select Tasks from the database");
        lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 15));
        lblNewLabel_1.setBounds(14, 11, 250, 18);
        contentPane.add(lblNewLabel_1);


        final JCheckBox chckbxNewCheckBox = new JCheckBox("running");
        chckbxNewCheckBox.setBounds(38, 692, 115, 23);
        contentPane.add(chckbxNewCheckBox);

        final JCheckBox chckbxNewCheckBox_1 = new JCheckBox("completed");
        chckbxNewCheckBox_1.setBounds(38, 718, 115, 23);
        contentPane.add(chckbxNewCheckBox_1);

        final JCheckBox chckbxNewCheckBox_2 = new JCheckBox("error");
        chckbxNewCheckBox_2.setBounds(38, 744, 115, 23);
        contentPane.add(chckbxNewCheckBox_2);

        final JCheckBox chckbxNewCheckBox_3 = new JCheckBox("notYetRun");
        chckbxNewCheckBox_3.setBounds(38, 770, 115, 23);
        contentPane.add(chckbxNewCheckBox_3);


        final JLabel todayLbl = new JLabel("This is not today's date !");
        todayLbl.setBounds(850, 62, 160, 14);
        contentPane.add(todayLbl);
        todayLbl.setForeground(Color.RED);
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");

        if (formatter.format(opsModeMainFrame.dateToSave).equalsIgnoreCase(formatter.format(Calendar.getInstance().getTime())))
        {
            todayLbl.setVisible(false);

        } else
        {
            todayLbl.setVisible(true);
        }

        /*shows the "This is not today's date" label when any other date other than today is chosen by the datechooser */
        datechooser.addPropertyChangeListener(new PropertyChangeListener()
        {

            public void propertyChange(PropertyChangeEvent evt)
            {
                SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");


                if (formatter.format(Calendar.getInstance().getTime()).equalsIgnoreCase(formatter.format(datechooser.getDate())))
                {
                    todayLbl.setVisible(false);

                } else
                {
                    todayLbl.setVisible(true);
                }
            }
        });


        JButton setToday = new JButton("Today");

        /*to set date to Today using Today button */
        setToday.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {


                datechooser.setDate(Calendar.getInstance().getTime());
                opsModeMainFrame.dateToSave = datechooser.getDate();

            }
        });

        setToday.setBounds(918, 83, 109, 23);
        contentPane.add(setToday);

        JButton subDateButton = new JButton("");
        subDateButton.setIcon(new ImageIcon(JobMonitorOpenerFrame.class.getResource("/Resources/Icons/arrow_left.png")));
        subDateButton.setToolTipText("Click to subtract a date");
        subDateButton.setBounds(820, 24, 30, 23);
        contentPane.add(subDateButton);

        /*Subtracting the date by 1 day */
        subDateButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                datechooser.setDate(new java.sql.Date(datechooser.getDate().getTime() - 60 * 60 * 24 * 1000));
                opsModeMainFrame.dateToSave = datechooser.getDate();


            }
        });

        btnSubmit.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {

                Date selectedDate = datechooser.getDate();

                if (selectedDate == null)
                {
                    Calendar cal = Calendar.getInstance();
                    selectedDate = cal.getTime();

                }
                opsModeMainFrame.dateToSave = datechooser.getDate();

                List<String> jobcatlist = new ArrayList<String>();


                if (chckbxNewCheckBox.isSelected())
                {
                    jobcatlist.add(chckbxNewCheckBox.getText());
                }
                if (chckbxNewCheckBox_1.isSelected())
                {
                    jobcatlist.add(chckbxNewCheckBox_1.getText());
                }
                if (chckbxNewCheckBox_2.isSelected())
                {
                    jobcatlist.add(chckbxNewCheckBox_2.getText());
                }
                if (chckbxNewCheckBox_3.isSelected())
                {
                    jobcatlist.add(chckbxNewCheckBox_3.getText());
                }


                Object[] selectedrolesValues = rolelist.getSelectedValues();
                int[] selectedUserRoles = new int[selectedrolesValues.length];

                int graphWidth = Integer.parseInt(XDim.getSelectedItem().toString());
                int graphHeight = Integer.parseInt(YDim.getSelectedItem().toString());


                for (int i = 0; i < selectedUserRoles.length; i++)
                {

                    selectedUserRoles[i] = roleMap.get(selectedrolesValues[i].toString());

                }

                Object[] selectedjobs = joblist.getSelectedValues();
                Object[] jobcategory = jobcatlist.toArray();
                String selectedLib = comboBox.getSelectedItem().toString();

                //quick horrible way to remember the library selected...works only for these two
                if (selectedLib.equals("JGraphX"))
                {
                    opsModeMainFrame.libToSave = new String[]{"JGraphX", "GraphViz"};
                } else
                {
                    opsModeMainFrame.libToSave = new String[]{"GraphViz", "JGraphX"};
                }

                selectedCategory = Arrays.copyOf(jobcategory, jobcategory.length, String[].class);

                String[] selectedUserJobs = Arrays.copyOf(selectedjobs, selectedjobs.length, String[].class);

                IntFrame nf = new IntFrame(selectedUserJobs, selectedDate, selectedCategory, selectedUserRoles, selectedLib, user, tb, dtp, opsModeMainFrame.signoffMap, graphWidth, graphHeight);

                setVisible(false);

            }
        });

        /* to show the dimension drop downs appear when you choose GraphViz */
        comboBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (comboBox.getSelectedItem().toString().equalsIgnoreCase("GraphViz"))
                {

                    XdimLabel.setVisible(true);
                    YdimLabel.setVisible(true);
                    XDim.setVisible(true);
                    YDim.setVisible(true);
                } else
                {
                    XdimLabel.setVisible(false);
                    YdimLabel.setVisible(false);
                    XDim.setVisible(false);
                    YDim.setVisible(false);

                }
            }
        });


    }


}
